package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;



    //新增分类
    @PostMapping
    public R saveGroup(@RequestBody Category category) {
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser((Long) session.getAttribute("loginUser"));
        category.setUpdateUser((Long) session.getAttribute("loginUser"));*/

        categoryService.save(category);

        return R.success("新增套餐成功");
    }

    //分页
    @GetMapping("/page")
    public R selectByPage(Integer page, Integer pageSize) {

        IPage<Category> pageinfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Category::getSort);
        categoryService.page(pageinfo, qw);

        List<Category> records = pageinfo.getRecords();
        Integer total = Math.toIntExact(pageinfo.getTotal());

        Map data = new HashMap();
        data.put("records", records);
        data.put("total", total);

        return R.success(data);
    }

    //删除
    @DeleteMapping
    public R deleteById(Long id){
        categoryService.remove(id);
        return R.success("删除分类成功");
    }

    //修改
    @PutMapping
    public R updateGroup(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    //查询菜品分类列表数据
    @GetMapping("/list")
    public R list(Integer  type){
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(type!=null,Category::getType,type);
        qw.orderByDesc(Category::getSort);
        List<Category> list = categoryService.list(qw);
        return R.success(list);
    }






























}
